<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <p>loading</p>
</body>
<script>
    /*getData("https://api.github.com/search/repositories?q=js&sort=stars&order=desc").then((res)=>{
        var response = JSON.parse(res);
        var p = document.querySelector("p");
        p.style.display="none";
        var divNode = document.createElement("div");
        divNode.innerHTML=`github上最流行的库是<a href="${response.items[0].html_url}">${response.items[0].name}</a>`
        document.body.appendChild(divNode)
    })
*/
    function getData(url) {
      return  new Promise((resolve,reject)=>{
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange=()=>{
                if(xhr.readyState === 4){
                    if(xhr.status===200 || xhr.status===304 ){
                        resolve(xhr.responseText)
                    }else {
                        reject("请求失败")
                    }
                }
            }
            xhr.open("get",url);
            xhr.send();
        })
    }

    (async ()=>{
        var res = await getData("https://api.github.com/search/repositories?q=js&sort=stars&order=desc");
        var response = JSON.parse(res);
        var p = document.querySelector("p");
        p.style.display="none";
        var divNode = document.createElement("div");
        divNode.innerHTML=`github上最流行的库是<a href="${response.items[0].html_url}">${response.items[0].name}</a>`
        document.body.appendChild(divNode)
    })()
</script>
</html>